package com.qijian.common.utils.sync;

import com.alibaba.fastjson.JSONObject;
import com.qijian.common.annotation.KingDeeField;
import lombok.Data;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

@Component
@RequiredArgsConstructor
public class KingDeeFile extends BaseKingDee {

    private final static String SalesKey = "SAL_SaleOrder";

    public List<JSONObject> getSalesContractFile(Collection<Long> ids) {
        return super.getFile(SalesKey, ids);
    }

    public List<KingDeeFileBean> getSalesKingDeeFile(Collection<Long> salesIds) {
        List<JSONObject> salesContractFile = getSalesContractFile(salesIds);
        return salesContractFile.stream().map(f -> {
            KingDeeFileBean fileBean = new KingDeeFileBean();
            fileBean.setId(f.getLong("FID"));
            fileBean.setFileId(f.getString("FFileId"));
            fileBean.setEntryId(f.getLong("FInterID"));
            fileBean.setFileName(f.getString("FAttachmentName"));
            fileBean.setIsAllowDownload(f.getBoolean("FIsAllowDownload"));
            fileBean.setBillType(f.getString("FBillType"));
            return fileBean;
        }).collect(Collectors.toList());
    }


    @Data
    public static class KingDeeFileBean {
        @KingDeeField("FID")
        private Long id;
        @KingDeeField("FFileId")
        private String fileId;
        @KingDeeField("FInterID")
        private Long entryId;
        @KingDeeField("FAttachmentName")
        private String fileName;
        @KingDeeField("FIsAllowDownload")
        private Boolean isAllowDownload;
        @KingDeeField("FBillType")
        private String billType;
    }

    public List<KingDeeFileBean> getKingDeeFile(String billType, Collection<Long> ids) {
        List<JSONObject> salesContractFile = getFile(billType, ids);
        return salesContractFile.stream().map(f -> {
            KingDeeFileBean fileBean = new KingDeeFileBean();
            fileBean.setId(f.getLong("FID"));
            fileBean.setFileId(f.getString("FFileId"));
            fileBean.setEntryId(f.getLong("FInterID"));
            fileBean.setFileName(f.getString("FAttachmentName"));
            fileBean.setIsAllowDownload(f.getBoolean("FIsAllowDownload"));
            fileBean.setBillType(f.getString("FBillType"));
            return fileBean;
        }).collect(Collectors.toList());
    }

    public List<JSONObject> getFile(String billType, Collection<Long> ids) {
        return super.getFile(billType, ids);
    }

}
